<?php
namespace app\index\service;


use app\index\model\ContractBuModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\EnginTingModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\IncomeEnginModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaoShouModel;
use app\index\model\UserModel;
use think\Db;

class EnginService extends CommonService
{

    public function get_engin_state(){
        return [
            1=>'预承揽',
            3=>'在建',
            5=>'合同竣工(人员释放)',
            7=>'完结',
        ];
    }


    public function get_engin_state_name($data){
        $name = '';
        if($data['state'] == 1){
            $name = '预承揽';
        }
        if($data['state'] == 3){
            $name = '在建';
        }
        if($data['state'] == 5){
            $name = '合同竣工(人员释放)';
        }
        if($data['state'] == 7){
            $name = '完结';
        }
        return $name;
    }

    public function get_engin_state_name_tag($data){
        $name = '';
        if($data['state'] == 1){
            $name = '<span class="colorhong"></span>预承揽';
        }
        if($data['state'] == 3){
            $name = '<span class="colorlv"></span>在建';
        }
        if($data['state'] == 5){
            $name = '<span class="colorzi"></span>合同竣工(人员释放)';
        }
        if($data['state'] == 7){
            $name = '<span class="colorhuang"></span>完结';
        }
        return $name;
    }

    public function get_engin_lantake(){
        return [
            1=>'预承揽工程',
            2=>'正式承揽工程',
        ];
    }
    public function get_engin_lantake_name($lantake){
        $arr = $this->get_engin_lantake();
        $str = isset($arr[$lantake]) ? $arr[$lantake] : '';
        return $str;
    }


    public function check_engin_name($name, $id=0){
        return !!EnginModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in', [0,1])
            ->where('id','neq', $id)->find();
    }

    public function get_engin_showinfo($enginid){
        $data = EnginModel::where(['id'=>$enginid])->find();
        if(!$data){
            return $data;
        }

        $data['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->sum('money');


        $data['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['name'=>$data['gofuer']])->value('liantel');

        $data['state_name'] = $this->get_engin_state_name($data);

        $data['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $data['lanshi']);
        $data['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $data['kailei']);
        $data['gui_unit_name'] = DeptModel::get_name_by_pk($data['gui_unit']);

        $data['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$enginid])->find();
        $data['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$enginid])->find();

        $data['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($enginid);
        $data['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($enginid);
        $data['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($enginid);
        $data['hougonghuikeyong'] = round($data['hougonghuimoney'] - $data['hougongzhimoneyquben'] - $data['hougonghuikoufee'], 2);



        $gofuerinfo = [];
        $gofuerinfo['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($data['gofuer']);
        $gofuerinfo['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($data['gofuer']);
        $gofuerinfo['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($data['gofuer']);
        $gofuerinfo['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($data['gofuer']);
        $gofuerinfo['hougongyukeyong'] = round($gofuerinfo['hougongyumoney']+$gofuerinfo['hougongshuimoneygoquan']-$gofuerinfo['hougongyukoufee']-$gofuerinfo['hougongzhimoneyqugoer'],2);
        $data['gofuerinfo'] = $gofuerinfo;


        $gofuerxianginfo = EnginModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$data['gofuer']])->select()->toArray();
        foreach ($gofuerxianginfo as &$govv){
            $govv['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($govv['id']);
            $govv['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($govv['id']);
            $govv['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($govv['id']);
            $govv['hougonghuikeyong'] = round($govv['hougonghuimoney'] - $govv['hougongzhimoneyquben'] - $govv['hougonghuikoufee'], 2);
        }
        $data['gofuerxianginfo'] = $gofuerxianginfo;


        $data['piaokaifee'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
            ->where(['kai.enginid'=>$data['id']])
            ->sum('list.money');

        //当前工程的工程销项票金额÷【结算 > 施工合同金额 > 中标价】×100%
        $data['leixiaobi'] = 0;
        if($data['shijiefee'] > 0){
            $data['leixiaobi'] = round($data['piaokaifee'] / $data['shijiefee'] * 100, 2);
        }else{
            if($data['shiinfo'] && $data['shiinfo']['money'] > 0){
                $data['leixiaobi'] = round($data['piaokaifee'] / $data['shiinfo']['money'] * 100, 2);
            }else{
                if($data['winfee'] > 0){
                    $data['leixiaobi'] = round($data['piaokaifee'] / $data['winfee'] * 100, 2);
                }
            }
        }


        //累计工程回款÷【结算 > 施工合同金额 > 中标价】X100%
        $data['leihuibi'] = 0;
        if($data['shijiefee'] > 0){
            $data['leihuibi'] = round($data['hougonghuimoney'] / $data['shijiefee']*100, 2);
        }else{
            if($data['shiinfo'] && $data['shiinfo']['money'] > 0){
                $data['leihuibi'] = round($data['hougonghuimoney'] / $data['shiinfo']['money']*100, 2);
            }else{
                if($data['winfee'] > 0){
                    $data['leihuibi'] = round($data['hougonghuimoney'] / $data['winfee']*100, 2);
                }
            }
        }

        $data['jiedongkoufee'] = HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$data['id'],'isdong'=>1])
            ->sum('list.koufee');

        $data['jiedongkoufee'] += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$data['id'],'isdong'=>1])
            ->sum('list.koufee');

        $data['gonghui_keyu'] = round($data['hougonghuimoney'] - $data['hougongzhimoneyquben'] - $data['hougonghuikoufee'] + $data['jiedongkoufee'], 2);




        $data['caifee'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['id']])->sum('money');
        $data['caijiefee'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['id']])->sum('money');
        $data['piaoshoufee'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['id']])->sum('money');
        $data['piaoshouratefee'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['id']])->sum('ratefee');
        $data['piaokaifee'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
            ->where(['kai.enginid'=>$data['id']])
            ->sum('list.money');
        $data['piaokairatefee'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
            ->where(['kai.enginid'=>$data['id']])
            ->sum('list.ratefee');

        $data['yingkoushuifee'] = 0;
        if($data['neiinfo']){
            $data['yingkoushuifee'] = round($data['piaokaifee']*$data['neiinfo']['rateshui']/100, 2);
        }

//        $data['yikoushuifee'] = HouKouShuifeiModel::alias('list')
//            ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
//            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
//            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
//            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
//            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
//            ->join('engin_shui shui','shui.id=list.shuiid')
//            ->join('engin e','e.id=shui.enginid')
//            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
//            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
//            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
//            ->where('hui.isturn|yu.isturn','=', 1)
//            ->where('hui.status|yu.status','=', 1)
//            ->where('list.houkoulei','=', 2)
//            ->sum('list.koufee');

        $data['yikoushuifee'] = HouKouShuifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1,'shui.enginid'=>$data['id']])
            ->sum('list.koufee');
        $data['yikoushuifee'] += HouKouShuifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2,'shui.enginid'=>$data['id']])
            ->sum('list.koufee');

        $data['yikoushuifee'] += HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1,'list.houkoulei'=>2,'list.enginid'=>$data['id']])
            ->sum('list.koufee');
        $data['yikoushuifee'] += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2,'list.houkoulei'=>2,'list.enginid'=>$data['id']])
            ->sum('list.koufee');

        $data['daikoushuifee'] = round($data['yingkoushuifee'] - $data['yikoushuifee'], 2);
        $data['daikoushuifee'] = $data['daikoushuifee'] < 0 ? 0 : $data['daikoushuifee'];
        $data['leigongshuifee'] = HouGongshuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['id']])->sum('money');

        return $data;
    }




    public function get_engin_shui_state(){
        return [
            0=>'待签订',
            1=>'已签订',
        ];
    }
    public function get_engin_shui_state_name($state){
        $name = '';
        if($state == 0){
            $name = '待签订';
        }
        if($state == 1){
            $name = '已签订';
        }
        return $name;
    }






    public function get_engin_incomeengin($enginid){
        $fee = IncomeEnginModel::where(['deleted_user'=>0,'mal'=>1,'enginid'=>$enginid])->sum('money');
        return $fee;
    }

    public function get_engin_paymentengin($enginid){
        $fee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_paymentbao($enginid){
        $fee = PaymentBaoModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_paymentneikou($enginid){
        $fee = PaymentNeikouModel::where(['deleted_user'=>0,'enginid'=>$enginid])->sum('money');
        return $fee;
    }

    public function get_engin_paymentneifu($enginid){
        $fee = PaymentNeifuModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_balance($enginid){
        $income = $this->get_engin_incomeengin($enginid);
        $zhi = $this->get_engin_paymentengin($enginid);
        $zhi += $this->get_engin_paymentbao($enginid);
        $zhi += $this->get_engin_paymentneikou($enginid);
        $zhi += $this->get_engin_paymentneifu($enginid);
        $balance = $income - $zhi;
        return $balance;
    }




    //施工合同合同金额  -  审核完成
    public function get_contract_shi_money($enginid){
        $money = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }
    //施工合同结算金额  -  审核完成
    public function get_contract_shi_jie_money($enginid){
        $money = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }

    public function get_contract_shi_bu_money_byshi($shiid){
        $money = ContractBuModel::where(['shiid'=>$shiid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }
    //施工合同补充合同金额  -  审核完成
    public function get_contract_shi_bu_money($enginid){
        $shiid = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->value('id');
        $money = ContractBuModel::where(['shiid'=>$shiid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }
    //施工合同变更后金额  -  审核完成
    public function get_contract_shi_final_money($enginid){
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->find();
        $money = $shiinfo['money'];
        $money += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }





    public function get_user_engin(){
        $str = "find_in_set(".$this->userinfo['id'].",man_zhuan)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zi)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_cheng)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zhian)";
        $where = Db::raw($str);
        $arr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where($where)
            ->column('id');
        return $arr;
    }




    public function get_engin_buture(){
        return ['新建','扩建','改建','迁建','恢复'];
    }

    public function get_engin_yanjie(){
        return ['通过','未通过'];
    }


}
